package JDBC.jdbc;

import java.sql.*;
import java.util.ArrayList;


public class Query {
//    封装查询
    public static void main(String[] args)throws Exception {
//      注册驱动，建立链接
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "123456");

        ArrayList<account> list = new ArrayList<>();
//        查询操作
        String sql = "select *from account where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,1);
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int money = resultSet.getInt("money");
            //System.out.println("id="+id+",name="+name+",money="+money);
           account account = new account();
            account.setId(id);
            account.setName(name);
            account.setMoney(money);
//            System.out.println(account);
//          封装操作
            list.add(account);
        }




//        释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
//        遍历集合
        for (account account : list) {
            System.out.println(account);
        }
    }
}
